﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCCommon.Enums;
using BCCommon.TMS.Order;
using BCEntity.Sim.Admin.Admin;
using BCEntity.TMS.Order.OrderPool;
using Dapper;

namespace BCData.TMS.Order.OrderPool
{
    public class OrderPoolData : IOrderPoolData
    {
        private const string QUERY = @"SELECT A.OrderId,G.OrderNo, A.AssignTimeOut, A.AssignStatus, A.DriverId,A.VehicleNo, A.ProvinceCode, A.CityCode, A.CountyCode, A.Editer, 
		                                        A.CreateTime,A.UpdateTime,A.EndTime,A.IsTimeOut,A.TimeOutLevel,A.Distance,A.AdminId,G.DriverName,G.DriverPhone,
                                                A.AssignTimeOut,A.AssignType,A.IsAssignReady,
                                                CASE WHEN F.OrderId IS NULL THEN 0 ELSE 1 END AS AssignRecordExists
                                        FROM OrderPool A
                                        LEFT JOIN OrderAssignRecord F ON A.OrderId=F.OrderId 
                                        LEFT JOIN `Orders` G ON A.OrderId=G.OrderId ";
        private readonly IDatabaseContext databaseContext;

        public OrderPoolData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<OrderPoolEntity> Add(OrderPoolEntity entity)
        {
            string sql = @"INSERT INTO OrderPool(OrderId, ProvinceCode, CityCode, CountyCode, DepartmentId, DepartmentName, CompanyId, CompanyName,FenceId, AgentRecordId,AssignTimeOut,AssignType,IsAssignReady)
                            VALUE(?OrderId,?ProvinceCode,?CityCode,?CountyCode, ?DepartmentId, ?DepartmentName, ?CompanyId, ?CompanyName,?FenceId, ?AgentRecordId,?AssignTimeOut,?AssignType,?IsAssignReady); ";
            sql = string.Format("{0} {1} WHERE A.OrderId=?OrderId LIMIT 1;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", entity.OrderId, DbType.Int64);
            parameters.Add("?ProvinceCode", entity.ProvinceCode, DbType.AnsiString, size: 20);
            parameters.Add("?CityCode", entity.CityCode, DbType.AnsiString, size: 20);
            parameters.Add("?CountyCode", entity.CountyCode, DbType.AnsiString, size: 20);
            parameters.Add("?DepartmentId", entity.DepartmentId, DbType.Int32);
            parameters.Add("?DepartmentName", entity.DepartmentName, DbType.AnsiString, size: 50);
            parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32);
            parameters.Add("?CompanyName", entity.CompanyName, DbType.AnsiString, size: 50);
            parameters.Add("?FenceId", entity.FenceId, DbType.Int64);
            parameters.Add("?AgentRecordId", entity.AgentRecordId, DbType.Int64);
            parameters.Add("?AssignTimeOut", entity.AssignTimeOut, DbType.DateTime);
            parameters.Add("?AssignType", entity.AssignType.ToString(), DbType.AnsiString);
            parameters.Add("?IsAssignReady", entity.IsAssignReady, DbType.Boolean);
            return await this.databaseContext.Master.QueryEntityAsync<OrderPoolEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> ClearOrderAssign(long orderId)
        {
            string sql = SqlCommands.GetUpdateSql("OrderPool", new string[] { "OrderId", "AssignTimeOut", "AssignStatus",
                "DriverId","VehicleNo","DriverName","DriverPhone" }, new string[] { "OrderId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderId, DbType.Int64, size: 20);
            parameters.Add("?AssignTimeOut", DateTime.Now.AddMinutes(20), DbType.DateTime);
            parameters.Add("?AssignStatus", OrderAssignHandleStatus.Pending.ToString(), DbType.AnsiString);
            parameters.Add("?DriverId", null, DbType.Int64, size: 20);
            parameters.Add("?VehicleNo", null, DbType.AnsiString, size: 20);
            parameters.Add("?DriverName", null, DbType.AnsiString, size: 20);
            parameters.Add("?DriverPhone", null, DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters,databaseContext.Transaction)>0;
        }

        public void UpdateTimeOutOrderAndClearAssign()
        {
            throw new NotImplementedException();
            
        }

        public async Task<(IEnumerable<OrderPoolEntity>, int)> Query(long? adminId, string provinceCode, string cityCode, string countyCode, OrderAssignHandleStatus? handleStatus, long? driverId, DateTime? start, DateTime? end, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            wheres.Add("A.IsAssignReady=?IsAssignReady");
            parameters.Add("?IsAssignReady", true, DbType.Boolean);

            if (!string.IsNullOrWhiteSpace(provinceCode))
            {
                wheres.Add("A.ProvinceCode=?ProvinceCode");
                parameters.Add("?ProvinceCode", provinceCode, DbType.AnsiString, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(cityCode))
            {
                wheres.Add("A.CityCode=?CityCode");
                parameters.Add("?CityCode", cityCode, DbType.AnsiString, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(countyCode))
            {
                wheres.Add("A.CountyCode=?CountyCode");
                parameters.Add("?CountyCode", countyCode, DbType.AnsiString, size: 20);
            }
            if (driverId.HasValue)
            {
                wheres.Add("A.DriverId=?DriverId");
                parameters.Add("?DriverId", driverId, DbType.Int64, size: 20);
            }
            if (handleStatus.HasValue)
            {
                wheres.Add("A.AssignStatus=?AssignStatus");
                parameters.Add("?AssignStatus", handleStatus.Value.ToString(), DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("A.CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("A.CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("A.CreateTime>=?Start AND A.CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }


            string sql = string.Format(@"{0} {1} GROUP BY A.OrderId ORDER BY A.CreateTime {4},A.TimeOutLevel {4}  LIMIT {2},{3}; 
                                        SELECT COUNT(1) FROM(
                                            SELECT OrderId
                                            FROM OrderPool A {1} GROUP BY A.OrderId)T",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex - 1) * pageSize,
                  pageSize,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryPageableListAsync<OrderPoolEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<OrderPoolEntity>, int)> Query(List<long> listAdminId, string provinceCode, string cityCode, string countyCode, OrderAssignHandleStatus? handleStatus, long? driverId, DateTime? start, DateTime? end, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            wheres.Add("A.IsAssignReady=?IsAssignReady");
            parameters.Add("?IsAssignReady", true, DbType.Boolean);

            if (!string.IsNullOrWhiteSpace(provinceCode))
            {
                wheres.Add("A.ProvinceCode=?ProvinceCode");
                parameters.Add("?ProvinceCode", provinceCode, DbType.AnsiString, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(cityCode))
            {
                wheres.Add("A.CityCode=?CityCode");
                parameters.Add("?CityCode", cityCode, DbType.AnsiString, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(countyCode))
            {
                wheres.Add("A.CountyCode=?CountyCode");
                parameters.Add("?CountyCode", countyCode, DbType.AnsiString, size: 20);
            }
            if (driverId.HasValue)
            {
                wheres.Add("A.DriverId=?DriverId");
                parameters.Add("?DriverId", driverId, DbType.Int64, size: 20);
            }
            if (handleStatus.HasValue)
            {
                wheres.Add("A.AssignStatus=?AssignStatus");
                parameters.Add("?AssignStatus", handleStatus.Value.ToString(), DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("A.CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("A.CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("A.CreateTime>=?Start AND A.CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else
            {
                wheres.Add("A.CreateTime>=?Start AND A.CreateTime<=?End");
                parameters.Add("?Start", DateTime.Now.AddMonths(-1), DbType.DateTime);
                parameters.Add("?End", DateTime.Now, DbType.DateTime);
            }
            if (listAdminId != null && listAdminId.Count > 0)
            {
                string fstr = string.Empty;
                listAdminId.ForEach(s => fstr += (s + ","));
                fstr = fstr.TrimEnd(',');
                wheres.Add($"B.AdminId IN ({fstr}) ");
            }

            string sql = string.Format(@"{0} {1} GROUP BY A.OrderId ORDER BY A.CreateTime {4},A.TimeOutLevel {4}  LIMIT {2},{3}; 
                                        SELECT COUNT(1) FROM(
                                            SELECT OrderId
                                            FROM OrderPool A
                                            {1} GROUP BY A.OrderId)T",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex - 1) * pageSize,
                  pageSize,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryPageableListAsync<OrderPoolEntity>(sql, parameters);
        }

        public async Task<bool> UpdateStatusDone(long orderId, long? driverId)
        {
            string sql = SqlCommands.GetUpdateSql("OrderPool", new string[] { "OrderId", "AssignTimeOut", "AssignStatus",
                "DriverId","EndTime" }, new string[] { "OrderId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderId, DbType.Int64, size: 20);
            parameters.Add("?AssignTimeOut", null, DbType.DateTime);
            parameters.Add("?AssignStatus", OrderAssignHandleStatus.Done.ToString(), DbType.AnsiString);
            parameters.Add("?DriverId", driverId, DbType.Int64, size: 20);
            parameters.Add("?EndTime", DateTime.Now, DbType.DateTime);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters,databaseContext.Transaction)>0;
        }

        public async Task<bool> UpdateStatusProcessing(long adminId, string editer, long orderId, long driverId,string vehicleNo, double distance)
        {
            string sql = @"UPDATE OrderPool SET
                                AssignTimeOut=?AssignTimeOut,
                                AssignStatus=?AssignStatus,
                                DriverId=?DriverId,
                                VehicleNo=?VehicleNo,
                                Distance=?Distance,
                                Editer=?Editer,
                                AdminId=?AdminId,
                                AssignTime=?AssignTime
                            WHERE OrderId=?OrderId;";
            sql = string.Format(@"{0} UPDATE `Orders` SET AssignEditerId=?AssignEditerId,AssignEditer=?AssignEditer,AssignTime=?AssignTime WHERE OrderId=?OrderId;", sql);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderId, DbType.Int64, size: 20);
            parameters.Add("?AssignTimeOut", DateTime.Now.AddMinutes(20), DbType.DateTime);
            parameters.Add("?AssignStatus", OrderAssignHandleStatus.Processing.ToString(), DbType.AnsiString);
            parameters.Add("?DriverId", driverId, DbType.Int64, size: 20);
            parameters.Add("?VehicleNo", vehicleNo, DbType.AnsiString, size: 20);
            parameters.Add("?Distance", distance, DbType.Decimal);
            parameters.Add("?Editer", editer, DbType.AnsiString, size: 20);
            parameters.Add("?AdminId", adminId, DbType.Int64);
            parameters.Add("?AssignEditerId", adminId, DbType.Int64);
            parameters.Add("?AssignEditer", editer, DbType.AnsiString, size: 20);
            parameters.Add("?AssignTime", DateTime.Now, DbType.DateTime);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters,databaseContext.Transaction)>0;
        }

        public async Task<OrderPoolEntity> Get(long orderId)
        {
            string sql = string.Format("{0} WHERE  A.OrderId=?OrderId GROUP BY A.OrderId ;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<OrderPoolEntity>(sql, parameters);
        }

        public async Task<OrderPoolEntity> GetCancelOrder(long orderId)
        {
            string querySql = @"SELECT A.OrderId, A.AssignTimeOut, A.AssignStatus,A.DriverId,A.VehicleNo, A.ProvinceCode, A.CityCode, A.CountyCode, A.Editer, 
		                                        A.CreateTime,A.UpdateTime,A.EndTime,A.IsTimeOut,A.TimeOutLevel,A.Distance
                                        FROM OrderPool A";
            string sql = string.Format("{0} WHERE A.OrderId=?OrderId;", querySql);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<OrderPoolEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<OrderPoolEntity>, int)> GetOrderPoolStatisticsAsync(int? companyId, int? departmentId, List<long> listAdminId, DateTime? start, DateTime? end, OrderAssignHandleStatus? assignStatus, int pageIndex, int pageSize)
        {
            string sql = @"SELECT A.OrderId, O.OrderNo, A.AssignTimeOut, A.AssignStatus,A.DriverId,A.VehicleNo, A.ProvinceCode, A.CityCode, A.CountyCode, A.Editer, 
		                A.CreateTime,A.UpdateTime,A.EndTime,A.IsTimeOut,A.TimeOutLevel,A.Distance,A.AdminId,A.VehicleNo,A.DriverName,A.DriverPhone,
                        O.ProvinceName,O.CityName,O.CountyName,O.GoodsName,O.GoodsWeight,O.GoodsVolume,O.GoodsLength,O.GoodsWidth,O.GoodsHeight,A.DepartmentId,
                        A.DepartmentName,A.CompanyId,A.CompanyName,O.Remark,O.IsCommunication,O.CommunicateContent,
                        O.OrderStatus,O.AssignTime,O.AssignTime,O.AssignEditerId,O.AssignEditer,
                        O.IsAllowOperate,O.CompanyIds,O.IsAllowOperateRemark
                        FROM OrderPool A
                        LEFT JOIN `Orders` O ON O.OrderId = A.OrderId";
            List<string> wheres = new List<string>()
            {
                "O.OrderStatus <> 'Closed'"
            };
            List<string> wheresTwo = new List<string>()
            {
                "O.OrderStatus <> 'Closed'"
            };
            DynamicParameters parameters = new DynamicParameters();

            if (listAdminId != null && listAdminId.Count > 0)
            {
                string fstr = string.Empty;
                listAdminId.ForEach(s => fstr += (s + ","));
                fstr = fstr.TrimEnd(',');
                wheres.Add($"A.AdminId IN ({fstr}) ");
                if (!assignStatus.HasValue && (companyId.HasValue || departmentId.HasValue))
                {
                    wheres.Add("A.AssignStatus <> 'Pending'");
                    wheresTwo.Add("A.AssignStatus = 'Pending'");
                }
            }
            if (companyId.HasValue)
            {
                if (assignStatus.HasValue)
                {
                    wheres.Add("A.CompanyId = ?CompanyId");
                    parameters.Add("?CompanyId", companyId.Value, DbType.Int32);
                }
                if (!assignStatus.HasValue && (companyId.HasValue || departmentId.HasValue))
                {
                    wheresTwo.Add("A.CompanyId = ?CompanyId");
                    parameters.Add("?CompanyId", companyId.Value, DbType.Int32);
                }
            }
            if (departmentId.HasValue)
            {
                if (assignStatus.HasValue)
                {
                    wheres.Add("A.DepartmentId = ?DepartmentId");
                    parameters.Add("?DepartmentId", departmentId.Value, DbType.Int32);
                }
                if (!assignStatus.HasValue && (companyId.HasValue || departmentId.HasValue))
                {
                    wheresTwo.Add("A.DepartmentId = ?DepartmentId");
                    parameters.Add("?DepartmentId", departmentId.Value, DbType.Int32);
                }
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("DATE(A.CreateTime)>=DATE(?Start)");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                if (!assignStatus.HasValue && (companyId.HasValue || departmentId.HasValue))
                {
                    wheresTwo.Add("DATE(A.CreateTime)>=DATE(?Start)");
                    parameters.Add("?Start", start.Value, DbType.DateTime);
                }
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("DATE(A.CreateTime)<=DATE(?End)");
                parameters.Add("?End", end.Value, DbType.DateTime);
                if (!assignStatus.HasValue && (companyId.HasValue || departmentId.HasValue))
                {
                    wheresTwo.Add("DATE(A.CreateTime)<=DATE(?End)");
                    parameters.Add("?End", end.Value, DbType.DateTime);
                }
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("DATE(A.CreateTime)>=DATE(?Start) AND DATE(A.CreateTime)<=DATE(?End)");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
                if (!assignStatus.HasValue && (companyId.HasValue || departmentId.HasValue))
                {
                    wheresTwo.Add("DATE(A.CreateTime)>=DATE(?Start) AND DATE(A.CreateTime)<=DATE(?End)");
                    parameters.Add("?Start", start.Value, DbType.DateTime);
                    parameters.Add("?End", end.Value, DbType.DateTime);
                }
            }
            else
            {
                wheres.Add("DATE(A.CreateTime) = DATE(NOW())");
                if (!assignStatus.HasValue && (companyId.HasValue || departmentId.HasValue))
                {
                    wheresTwo.Add("DATE(A.CreateTime) = DATE(NOW())");
                }
            }
            if (assignStatus.HasValue)
            {
                wheres.Add("A.AssignStatus = ?AssignStatus");
                parameters.Add("?AssignStatus", assignStatus.Value.ToString(), DbType.AnsiString);
            }

            if (!assignStatus.HasValue && (companyId.HasValue || departmentId.HasValue))
            {
                sql = string.Format(@"{0} {1} UNION {0} {2} ORDER BY CreateTime DESC LIMIT {3},{4};
                                    SELECT SUM(RN) FROM (SELECT COUNT(1) AS RN
                                    FROM OrderPool A
                                    LEFT JOIN `Orders` O ON O.OrderId = A.OrderId
                                    LEFT JOIN Driver D ON A.DriverId=D.DriverId
                                    LEFT JOIN `User` E ON D.UserId=E.UserId 
                                    INNER JOIN Province F ON A.ProvinceCode=F.ProvinceCode
                                    INNER JOIN City G ON A.CityCode=G.CityCode
                                    LEFT JOIN County H ON A.CountyCode=H.CountyCode {1} 
                                    UNION
                                    SELECT COUNT(1) AS RN
                                    FROM OrderPool A
                                    LEFT JOIN `Orders` O ON O.OrderId = A.OrderId
                                    LEFT JOIN Driver D ON A.DriverId=D.DriverId
                                    LEFT JOIN `User` E ON D.UserId=E.UserId 
                                    INNER JOIN Province F ON A.ProvinceCode=F.ProvinceCode
                                    INNER JOIN City G ON A.CityCode=G.CityCode
                                    LEFT JOIN County H ON A.CountyCode=H.CountyCode {2}) T",
                                    sql,
                                    wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                                    wheresTwo.Count > 0 ? " WHERE " + string.Join(" AND ", wheresTwo) : "",
                                    (pageIndex - 1) * pageSize, pageSize);
            }
            else
            {
                sql = string.Format(@"{0} {1} ORDER BY A.CreateTime DESC LIMIT {2},{3};
                                    SELECT COUNT(1)
                                    FROM OrderPool A
                                    LEFT JOIN `Orders` O ON O.OrderId = A.OrderId
                                    LEFT JOIN Driver D ON A.DriverId=D.DriverId
                                    LEFT JOIN `User` E ON D.UserId=E.UserId 
                                    INNER JOIN Province F ON A.ProvinceCode=F.ProvinceCode
                                    INNER JOIN City G ON A.CityCode=G.CityCode
                                    LEFT JOIN County H ON A.CountyCode=H.CountyCode {1}",
                                    sql,
                                    wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                                    (pageIndex - 1) * pageSize, pageSize);
            }
            return await this.databaseContext.Slave.QueryPageableListAsync<OrderPoolEntity>(sql, parameters);
        }


        public async Task<(IEnumerable<OrderPoolEntity>, int)> QueryDriverOrders(OrderAssignHandleStatus? processing, long driverId, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>()
            {
                "A.DriverId =?DriverId"
            };
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?DriverId", driverId, DbType.Int64, size: 20);
            if (processing.HasValue)
            {
                wheres.Add("A.AssignStatus=?AssignStatus");
                parameters.Add("?AssignStatus", processing.Value.ToString(), DbType.AnsiString);
            }

            string sql = string.Format(@"{0} {1} GROUP BY A.OrderId ORDER BY A.CreateTime,A.TimeOutLevel  LIMIT {2},{3}; 
                                        SELECT COUNT(1) FROM(
                                            SELECT OrderId
                                            FROM OrderPool A
                                            {1} GROUP BY A.OrderId)T",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex - 1) * pageSize,
                  pageSize);
            return await this.databaseContext.Slave.QueryPageableListAsync<OrderPoolEntity>(sql, parameters);
        }

        public async Task<bool> UpdateIsAssignReady(long orderId)
        {
            string sql = @"UPDATE OrderPool SET IsAssignReady=1 WHERE OrderId=?OrderId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters) > 0;
        }

        public async Task<bool> UpdateOrderReceived(long orderId)
        {
            string sql = SqlCommands.GetUpdateSql("OrderPool", new[] { "AssignStatus", "EndTime" }, new string[] { "OrderId" }); DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?EndTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?OrderId", orderId, DbType.Int64);
            parameters.Add("?AssignStatus", OrderAssignHandleStatus.Done.ToString(), DbType.AnsiString);
            return await databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction)>0;
        }
    }
}
